<?php

namespace app\api\service;

use app\common\exception\BusinessException;
use app\common\model\Category as CategoryModel;

/**
 * 分类服务
 */
class Category
{

    /**
     * 条件查询
     *
     */
    public function list($data)
    {
        $result = [];
        // 根据类型获取分类
        $list = CategoryModel::all(function ($query) use ($data) {
            if (!empty($data['type'])) {
                $query->where('type', $data['type']);
            }
            $query->where('status', 1)->order('sort', 'desc');
        });
        foreach ($list as $key => $item) {
            $result[] = [
                'id' => $item->id,
                'name' => $item->name,
                'type' => $item->type,
            ];
        }
        return $result;
    }
}
